<?php

namespace PAT\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use PAT\CommentBundle\Entity\Comment;

/**
 * PAT\ProductBundle\Entity\Product
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="PAT\ProductBundle\Entity\ProductRepository")
 */
class Product
{	
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;

    /**
     * @var string $descriptionShort
     *
     * @ORM\Column(name="descriptionShort", type="text")
     */
    protected $descriptionShort;

    /**
     * @var string $descriptionLong
     *
     * @ORM\Column(name="descriptionLong", type="text")
     */
    protected $descriptionLong;

    /**
     * @var float $priceHT
     *
     * @ORM\Column(name="priceHT", type="float")
     */
    protected $priceHT;

    /**
     * @var \DateTime $releaseDate
     *
     * @ORM\Column(name="releaseDate", type="date")
     */
    protected $releaseDate;
	
	/**
	 * @ORM\ManyToOne(targetEntity="PAT\ProductBundle\Entity\Category")
     * @ORM\JoinColumn(nullable=false)
     */
	protected $category;
	 
	 /**
	 * @ORM\ManyToOne(targetEntity="PAT\ProductBundle\Entity\Stock")
     * @ORM\JoinColumn(nullable=false)
     */
	protected $stock;
	
	/**
	 * @ORM\OneToOne(targetEntity="PAT\ProductBundle\Entity\Picture", cascade={"persist"})
	 */
	protected $picture;
	
	/**
	 * @var integer $quantity
	 *
	 * @ORM\Column(name="quantity", type="integer")
	 */
	protected $quantity;

   	 /**
     * @ORM\ManyToMany(targetEntity="PAT\OfferBundle\Entity\Offer")
     */
	protected $offers;
	
	/**
     * @ORM\OneToMany(targetEntity="PAT\CommentBundle\Entity\Comment", mappedBy="product")
     */
	 protected $comments;
	 
	/**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Product
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set descriptionShort
     *
     * @param string $descriptionShort
     * @return Product
     */
    public function setDescriptionShort($descriptionShort)
    {
        $this->descriptionShort = $descriptionShort;
    
        return $this;
    }

    /**
     * Get descriptionShort
     *
     * @return string 
     */
    public function getDescriptionShort()
    {
        return $this->descriptionShort;
    }

    /**
     * Set descriptionLong
     *
     * @param string $descriptionLong
     * @return Product
     */
    public function setDescriptionLong($descriptionLong)
    {
        $this->descriptionLong = $descriptionLong;
    
        return $this;
    }

    /**
     * Get descriptionLong
     *
     * @return string 
     */
    public function getDescriptionLong()
    {
        return $this->descriptionLong;
    }

    /**
     * Set priceHT
     *
     * @param float $priceHT
     * @return Product
     */
    public function setPriceHT($priceHT)
    {
        $this->priceHT = $priceHT;
    
        return $this;
    }

    /**
     * Get priceHT
     *
     * @return float 
     */
    public function getPriceHT()
    {
        return $this->priceHT;
    }

    /**
     * Set releaseDate
     *
     * @param \DateTime $releaseDate
     * @return Product
     */
    public function setReleaseDate($releaseDate)
    {
        $this->releaseDate = $releaseDate;
    
        return $this;
    }

    /**
     * Get releaseDate
     *
     * @return \DateTime 
     */
    public function getReleaseDate()
    {
        return $this->releaseDate;
    }

    /**
     * Set picture
     *
     * @param PAT\ProductBundle\Entity\Picture $picture
     * @return Product
     */
    public function setPicture(\PAT\ProductBundle\Entity\Picture $picture = null)
    {
        $this->picture = $picture;
    
        return $this;
    }

    /**
     * Get picture
     *
     * @return PAT\ProductBundle\Entity\Picture 
     */
    public function getPicture()
    {
        return $this->picture;
    }

    /**
     * Set quantity
     *
     * @param integer $quantity
     * @return Product
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;
    
        return $this;
    }

    /**
     * Get quantity
     *
     * @return integer 
     */
    public function getQuantity()
    {
        return $this->quantity;
    }
    
    
    /**
     * Set category
     *
     * @param PAT\ProductBundle\Entity\Category $category
     * @return Product
     */
    public function setCategory(\PAT\ProductBundle\Entity\Category $category)
    {
        $this->category = $category;
    
        return $this;
    }

    /**
     * Get category
     *
     * @return PAT\ProductBundle\Entity\Category 
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Set stock
     *
     * @param PAT\ProductBundle\Entity\Stock $stock
     * @return Product
     */
    public function setStock(\PAT\ProductBundle\Entity\Stock $stock)
    {
        $this->stock = $stock;
    
        return $this;
    }

    /**
     * Get stock
     *
     * @return PAT\ProductBundle\Entity\Stock 
     */
    public function getStock()
    {
        return $this->stock;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->offers = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
     * Add offers
     *
     * @param PAT\OfferBundle\Entity\Offer $offers
     * @return Product
     */
    public function addOffer(\PAT\OfferBundle\Entity\Offer $offers)
    {
        $this->offers[] = $offers;
    
        return $this;
    }

    /**
     * Remove offers
     *
     * @param PAT\OfferBundle\Entity\Offer $offers
     */
    public function removeOffer(\PAT\OfferBundle\Entity\Offer $offers)
    {
        $this->offers->removeElement($offers);
    }

    /**
     * Get offers
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getOffers()
    {
        return $this->offers;
    }

    /**
     * Add comments
     *
     * @param PAT\ProductBundle\Entity\Product $comments
     * @return Product
     */
    public function addComment(\PAT\CommentBundle\Entity\Comment $comments)
    {
        $this->comments[] = $comments;
		$comments->setProduct($this);
        return $this;
    }

    /**
     * Remove comments
     *
     * @param PAT\ProductBundle\Entity\Product $comments
     */
    public function removeComment(\PAT\CommentBundle\Entity\Comment $comments)
    {
        $this->comments->removeElement($comments);
		$comments->setProduct(null);
    }

    /**
     * Get comments
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getComments()
    {
        $comments = array();
        if($this->comments != null)
        {
            $i = 0;
            foreach($this->comments as $comment)
            {
                $comments[$i] = $comment;
                $i++;
            }
        }
        return array_reverse($comments);
    }
    
    private function roundToHalf($rate)
    {
        $ceil = ceil($rate); // On prend l'entier supérieur
        $half = $ceil - 0.5; // On en déduit le milieu
        
        if($rate >= ($half + 0.25)) return $ceil; // Si le chiffre est supérieur à .75
        else if($rate < $half - 0.25) return floor($rate); // Si le chiffre est inférieur à .25
        else return $half; // Le chiffre est compris entre .25 et .75
    }
    
    public function getAverageRate()
    {
        $averageRate = 0;
        if(count($this->comments) > 0)
        {
            $totalRate = 0;
            foreach($this->comments as $comment)
            {
                $totalRate += $comment->getRate();
            }
            $averageRate = $totalRate / count($this->comments);
        }
        
        $roundedAverageRate = $this->roundToHalf($averageRate);
        
        return Comment::rateToHtml($roundedAverageRate);
    }
}